package day_2022_9_to_12.Day;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/09/06 21:00
 **/
public class Day4_8_Offer62_LastRemaining {
    class Solution {
        public int lastRemaining(int n, int m) {
            List<Integer> res = new ArrayList<>();
            for(int i = 0; i < n; ++i) res.add(i);
            int index = 0;
            while(n > 1){
                index = (index + m -1) % n;
                res.remove(index);
                n--;
            }
            return res.get(0);
        }
    }
    class Solution2 {
        public int lastRemaining(int n, int m) {
            int index = 0;
            for(int i = 2; i <= n; ++i){
                index = (index + m) % i;
            }
            return index;
        }
    }
}